package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.FansListData;
import com.wbao.dianniu.data.HeadData;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.listener.IFansList;
import com.wbao.dianniu.listener.IFansListListener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Administrator on 2017/8/17.
 */

public class FansListManager implements IFansList {
    private IFansListListener mListener;
    private Context mContext;

    public FansListManager(Context context) {
        mContext = context;
    }

    @Override
    public void fansList(int accountId, int page, int rows) {
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACTION_ACCOUNT_FANS_LIST)
                    .addArgs("accountId", accountId)
                    .addArgs("page", page)
                    .addArgs("rows", rows);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new HttpUtil.MyResponse() {
            @Override
            public void responseSucess(Object obj) {
                List<HeadData> dat = JsonUtil.toObjectList(obj, HeadData.class);
                // 通知界面
                if (null != mListener)
                    mListener.onFansListSuccess(dat);
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                if (null != mListener)
                    mListener.onFansListFailure(errorCode, msg);
            }
        });
    }

    @Override
    public boolean addFansListListener(IFansListListener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeFansListListener(IFansListListener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }
}
